package com.fubang.pathlearning.titlebar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fubang.pathlearning.R;

/**
 * 创建者: YIN
 * 创建时间: 2017/7/27 9:28
 * 电子邮箱: yin_ruimin@foxmail.com
 * 描述:
 */

public class TitleBarView extends LinearLayout {
    private TitleViewHolder mViewHolder;
    private View mTitleView;
    private TypedArray typedArray;
    private boolean isLeftWhiteVisible;
    private boolean isContentVisible;
    private boolean isRightContentVisible;
    private String titleContent;
    private String rightContent;
    private int rightIcon;
    private int backgroundColor;
    private int titleColor;
    private boolean isRightIconVisible;
    private boolean isLeftBlackVisible;

    public TitleBarView(Context context) {
        this(context, null);
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
        init(context, attrs);
    }

    public TitleBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(final Context context, AttributeSet attrs) {
        int layoutId = R.layout.comm_title_bar;
        mTitleView = LayoutInflater.from(context).inflate(layoutId, null);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView);
        isLeftWhiteVisible = typedArray.getBoolean(R.styleable.TitleBarView_isLeftWhiteVisible, false);
        isLeftBlackVisible = typedArray.getBoolean(R.styleable.TitleBarView_isLeftBlackVisible, false);
        isContentVisible = typedArray.getBoolean(R.styleable.TitleBarView_isContentVisible, true);
        isRightContentVisible = typedArray.getBoolean(R.styleable.TitleBarView_isRightContentVisible, true);
        isRightIconVisible = typedArray.getBoolean(R.styleable.TitleBarView_isRightIconVisible, false);
        titleContent = typedArray.getString(R.styleable.TitleBarView_titleContent);
        rightContent = typedArray.getString(R.styleable.TitleBarView_rightContent);
        rightIcon = typedArray.getResourceId(R.styleable.TitleBarView_rightIcon, R.mipmap.ic_launcher);
        backgroundColor = typedArray.getColor(R.styleable.TitleBarView_backgroundColor, Color.parseColor("#c3f5fe"));
        titleColor = typedArray.getColor(R.styleable.TitleBarView_titleColor, Color.parseColor("#c37876"));


        addView(mTitleView, layoutParams);
        mViewHolder = new TitleViewHolder(this);
        mViewHolder.mLeftWhiteArrow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLeftWhiteVisible) {
                    if (callBack != null) {
                        callBack.onLeftClick(v);
                    }
                }
            }
        });
        mViewHolder.mLeftBlackArrow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLeftBlackVisible) {
                    if (callBack != null) {
                        callBack.onLeftClick(v);
                    }
                }
            }
        });
        mViewHolder.mRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.onRightClick(v);
                }
            }
        });

        initViewsVisible(isContentVisible, isRightContentVisible);
        if (isLeftWhiteVisible) {
            mViewHolder.mLeftWhiteArrow.setVisibility(View.VISIBLE);
        } else if (isLeftBlackVisible) {
            mViewHolder.mLeftBlackArrow.setVisibility(View.VISIBLE);
        }
        if (isContentVisible) {
            setTitleContent(titleContent);
        }
        if (isRightContentVisible) {
            setTitleRight(rightContent);
        }
        if (isRightIconVisible) {
            setIconRight(rightIcon);
        }
        setTitleBackground(backgroundColor);
        setTitleColor(titleColor);
        typedArray.recycle();
    }

    private void setTitleColor(int titleColor) {
        mViewHolder.mContent.setTextColor(titleColor);
    }

    public void initViewsVisible(boolean isContentVisible, boolean isRightVisible) {
        // 中间标题
        mViewHolder.mContent.setVisibility(isContentVisible ? View.VISIBLE : View.INVISIBLE);
        // 右侧返回
        mViewHolder.mRight.setVisibility(isRightVisible ? View.VISIBLE : View.INVISIBLE);
    }

    public void setTitleContent(String title) {
        if (!TextUtils.isEmpty(title)) {
            mViewHolder.mContent.setText(title);
        }
    }


    public void setTitleRight(String text) {
        if (!TextUtils.isEmpty(text)) {
            mViewHolder.mRightIcon.setVisibility(GONE);
            mViewHolder.mRightContent.setVisibility(VISIBLE);
            mViewHolder.mRightContent.setText(text);
        }
    }

    public void setIconRight(int resId) {
        mViewHolder.mRightContent.setVisibility(GONE);
        mViewHolder.mRightIcon.setVisibility(VISIBLE);
        mViewHolder.mRightIcon.setImageResource(resId);
    }

    public void setTitleBackground(int color) {
        mTitleView.setBackgroundColor(color);
    }

    private CallBack callBack;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        void onLeftClick(View v);

        void onRightClick(View v);
    }

    private static class TitleViewHolder {
        private final ImageView mRightIcon;
        private ImageView mLeftBlackArrow;
        private ImageView mLeftWhiteArrow;
        private TextView mContent;
        private RelativeLayout mRight;
        private TextView mRightContent;

        private TitleViewHolder(View v) {
            mLeftWhiteArrow = (ImageView) v.findViewById(R.id.comm_title_bar_left_arrow_white);
            mLeftBlackArrow = (ImageView) v.findViewById(R.id.comm_title_bar_left_arrow_black);
            mRight = (RelativeLayout) v.findViewById(R.id.comm_title_bar_right);
            mContent = (TextView) v.findViewById(R.id.comm_title_bar_content);
            mRightContent = (TextView) v.findViewById(R.id.comm_title_bar_right_content);
            mRightIcon = ((ImageView) v.findViewById(R.id.comm_title_bar_right_icon));
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
